package com.xly.webclick.cusview.fontview;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.view.KeyEvent;

import com.xly.webclick.R;
import com.xly.webclick.utils.FontTypeManager;


/**
 * Created by amos on 2018/5/8.
 */

public class FontEditText extends AppCompatEditText {

    public interface OnKeyPreImeEvent{
        boolean onKeyPreIme(int keyCode, KeyEvent event);
    }


    private OnKeyPreImeEvent keyPreImeEvent;

    public void setKeyPreImeEvent(OnKeyPreImeEvent keyPreImeEvent) {
        this.keyPreImeEvent = keyPreImeEvent;
    }

    public FontEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontEditText);
        if (typedArray != null) {

            int type = typedArray.getInt(R.styleable.FontEditText_fet_fontType, 0);

            switch (type) {
                case 1:
                    setTypeface(FontTypeManager.getRobotoMedium(context.getAssets()));
                    break;
                case 2:
                    setTypeface(FontTypeManager.getRobotoRegular(context.getAssets()));
                    break;
                case 3:
                    setTypeface(FontTypeManager.getRobotoBold(context.getAssets()));
                    break;
                case 4:
                    setTypeface(FontTypeManager.getRobotoBlack(context.getAssets()));
                    break;
            }
            typedArray.recycle();
        }
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyPreImeEvent != null) {
            return keyPreImeEvent.onKeyPreIme(keyCode, event);
        }
        return super.onKeyPreIme(keyCode, event);
    }
}
